package com.comp490.sprite;

import java.util.ArrayList;
import java.util.List;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Rect;

import com.comp490.bullet.Bullet;
import com.comp490.bullet.RoundBullet;
import com.comp490.pattern.SpiralPattern;
import com.comp490.shooter.GameView;

public class MediumEnemySprite extends EnemySprite
{
    public final static int LIGHT_BLUE = 0;
    public final static int BLUE = 1;
    public final static int RED = 2;
    public final static int GREEN = 3;

    private int bitmapHeight;
    private int bitmapWidth;
    private float rotationSpeed = 0.5f;
    private float originalX = -1;
    private float originalY = -1;

    public MediumEnemySprite(GameView gameView, int enemyColor, int path, float originalX, float originalY)
    {
        super(gameView, 5, 1000);
        Bitmap enemyShip = BitmapResource.MEDIUM_ENEMY_SHIP;

        bitmapWidth = (int) ((enemyShip.getWidth() / 4.0) + 0.5);
        bitmapHeight = (int) ((enemyShip.getHeight() / 4.0) + 0.5);
        setBitmap(enemyShip, bitmapWidth, bitmapHeight);

        spriteTop = enemyColor * bitmapHeight;

        collisionRadius = bitmapHeight < bitmapWidth ? bitmapHeight / 3
                : bitmapWidth / 3;

        src = new Rect(spriteLeft, spriteTop, spriteLeft
                + bitmapWidth, spriteTop + bitmapHeight);
        
        dst = new Rect((int) x, (int) y, (int) x + bitmapWidth,
                (int) y + bitmapHeight);
        this.path = path;
        firePoint = 2;
        buildBulletPatterns();
        this.originalX = originalX;
        this.originalY = originalY;
    }

    public void draw(Canvas canvas)
    {
        if(!explode)
        {
			if(!active)
				return;
			
			src.set(spriteLeft, spriteTop, spriteLeft
                    + bitmapWidth, spriteTop + bitmapHeight);
            dst.set((int) x, (int) y, (int) x + bitmapWidth,
                    (int) y + bitmapHeight);

            canvas.save();
            canvas.rotate(rotationSpeed++, x + bitmapWidth / 2, y
                    + bitmapHeight / 2);
            canvas.drawBitmap(bitmap, src, dst, shipIsHit ? BitmapResource.HIT_INDICATOR : null);
            canvas.restore();

            if(shipIsHit)
            	shipIsHit = false;
            if(fCount++ % 9 == 0)
            {
                if(spriteLeft + bitmapWidth >= bmpWidth)
                    spriteLeft = 0;
                else
                    spriteLeft += bitmapWidth;
            }
        }
        else
        {
            explode = mExplosionSprite.draw(canvas, x, y, ExplosionSprite.EXPLOSION_SEVEN);
        }
    }

    @Override
    public float getCenterBitmapX()
    {
        return centerBitmapX + x;
    }

    @Override
    public float getCenterBitmapY()
    {
        return centerBitmapY + y;
    }

    @Override
    public void reset()
    {
		hitPoints = 5;
        x = originalX;
        y = originalY;
        buildBulletPatterns();
        firePoint = 2;		
    }

    @Override
    public void updatePosition()
    {
		if(path == MOVE_STRAIGHT)
			moveLine(3, 180);
		else if(path == MOVE_SIN)
			moveSin(1, 230);
        
		if(!boundsCheck(gameView))
		{
			active = false;
		}
		checkFirePos();
    }

	@Override
	protected void checkFirePos()
	{
		switch(firePoint)
		{
			case 2:
					if(x < screenWidth * 5/6)
					{
						bulletPatterns.get(0).setCoordinates(x, getCenterBitmapY());
						gameView.getEnemy_bullet_patterns().add(bulletPatterns.get(0));
						bulletPatterns.remove(0);
						firePoint--;
					}
					break;
			case 1:
					if(x < screenWidth * 3/6)
					{
						bulletPatterns.get(0).setCoordinates(x, getCenterBitmapY());
						gameView.getEnemy_bullet_patterns().add(bulletPatterns.get(0));
						bulletPatterns.remove(0);
						firePoint--;
					}
					break;
			default:
					break;
		}
	}

	@Override
	protected void buildBulletPatterns()
	{
		for (int i = 0; i < 2; i++)
		{
			List<Bullet> bullets = new ArrayList<Bullet>(5);
			for (int j = 0; j < 5; j++)
			{
				Bullet b = new RoundBullet(0, 0, 1, BitmapResource.RED_PAINT, 6, false);
				b.setActive(true);
				bullets.add(b);
			}

			SpiralPattern cone = new SpiralPattern(x, y, 0.5f, 1, bullets, gameView, true);
			bulletPatterns.add(cone);
		}
	}
}